<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/18
 * Time: 下午3:00
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class MemberController extends AdminBaseController
{
    /**
     *客户列表
     */
    public function member_list()
    {
        //获取页page
        $pg = I('get.p');
        $word = I('get.word');
        //判断关键词是否为空
        $where = empty($word) ? '1=1': 'fd_member.account like "%'.$word.'%"';
        //判断page是否为空，如果为空则默认为1
        $pg = empty($pg) ? '1=1' : $pg;
        $info = M('Member_info'); // 实例化User对象
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list = $info->order('add_time')->join('left join fd_member on fd_member.id=fd_member_info.mid')->where($where)->page($pg.',2')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = M('Member')->where($where)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('title','客户列表');
        $this->display();
    }

    /**
     *添加客户
     */
    public function member_add()
    {
        if(IS_AJAX || IS_POST){
                //获取账号
                $account = I('post.account');
                if(empty($account)){
                    $this->ajaxReturn(['state' => 1, 'msg' => '账号不能为空']);
                }
                //获取密码
                $pawd = I('post.pawd');
                if(empty($pawd)){
                    $this->ajaxReturn(['state' => 1, 'msg' => '密码不能我空']);
                }
                //重复密码
                $repawd = I('post.repawd');
                if(empty($repawd)){
                    $this->ajaxReturn(['state' => 1, 'msg' => '重复密码不能为空']);
                }
                //判断密码与重复密码是否相同
                if($pawd != $repawd){
                    $this->ajaxReturn(['state' => 1, 'msg' => '密码与重复密码不一样']);
                }
                //获取名字
                $name = I('post.name');
                //获取客户端IP
                $cip = $_SERVER['REMOTE_ADDR'];
                //获取盐
                $salt = getRandomNumber();
                $member = M('Member');
                //判断账号是否存在
                $uid = $member->where(['account' =>$account])->find();
                if(!empty($uid)){
                    $this->ajaxReturn(['state'=>1,'msg'=>'该账号已存在']);
                }
                //开启事务
                $member->startTrans();
                $data =[
                    'account' => $account,
                    'password'=> md5($pawd.$salt),
                    'salt'    => $salt,
                    'reg_time'=> time(),
                    'ip'      => $cip

                ];
                $memberinfo =  $member->add($data);
                //判断是否添加失败则会滚
                if(!$memberinfo){
                    $member->rollback();
                    $this->ajaxReturn(['state' => 1,'msg'=>'添加失败']);
                }
                $memberid = $member->getLastInsID();
                $info = [
                    'mid' => $memberid,
                    'name' => $name,
                    'add_time' => time()
                ];
                $mbinfo = M('member_info')->add($info);
                //判断是否添加成功，成功则提交，失败则会滚
                if($mbinfo){
                    $member->commit();
                }else{
                    $member->rollback();
                    $this->ajaxReturn(['state' => 1,'msg'=>'添加失败']);
                }
                $this->ajaxReturn(['state' => 0, 'msg' => '添加成功'] );
            }
        $this->assign('title','添加客户');
        $this->display();
    }
    /**
     *删除客户
     */
    public function del()
    {
        if(IS_AJAX  || IS_POST){
            $id = I('post.id');
            $info = M('Member_info')->where(['mid'=>$id])->find();
            //判断id是否存在
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作']);
            }
            //判断该账号是否存在
            if(!$info){
                $this->ajaxReturn(['state'=>1,'msg'=>'该客户不存在']);
            }
            $member = M('Member');
            $member->startTrans();
            $member->where(['id'=>$id])->delete();
            if(!$member){
                $member->rollback();
            }
            $del = M('Member_info')->where(['mid'=>$id])->delete();
            if(!$del){
                $member->rollback();
                $this->ajaxReturn(['state'=> 1 ,'msg'=>'删除失败']);
            }
            $member->commit();
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功']);
        }
        $this->ajaxReturn(['state'=>1,'msg'=>'非法操作']);
    }

    /**
     *获取要修改的用户信息
     */
    public function edit()
    {
            //获取要修改用户的id
            $id = I('get.id');
            //判断id是否存在
            if (empty($id)) {
                $this->ajaxReturn(['state' => 1, 'msg' => '非法操作']);
            }
            $info = M('Member')->where(['id' => $id])->find();
            //判断是否有该用户
            if (!$info) {
                $this->ajaxReturn(['state' => 1, 'msg' => '用户不存在']);
            }
            $this->assign('info', $info);
            $this->assign('title', '修改客户');
            $this->display();
    }

    /**
     *修改用户信息
     */
    public function save()
    {
        if(IS_AJAX || IS_POST){
            $id = I('post.id');
            //判段要修改的id是否存在
            if(empty($id)){
                $this->ajaxReturn(['state' => 1 ,'msg' => '非法操作']);
            }
            $account = I('post.account');
            //判断要修改的账号是否存在
            if(empty($account)){
                $this->ajaxReturn(['state'=>1,'msg'=>'要修改的账号不能为空']);
            }
            $info = M('Member')->where(['id'=>$id])->find();
            //判断要修改的用户是否存在
            if(!$info){
                $this->ajaxReturn(['state'=>1,'msg'=>'要修改的用户不存在']);
            }
            $up_info = M('Member')->where(['id'=>$id])->setField(['account'=>$account]);
            //判断是否修改成功
            if($up_info === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改失败']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'修改成功']);
        }

    }

}